package Forum;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.*;

import ibatis.IbatisHelper;

import com.crystaldecisions.sdk.occa.report.formatteddefinition.model.Map;
import com.ibatis.sqlmap.client.SqlMapClient;

public class ForumThreadManager {
	private SqlMapClient ibatis;
	
	public ForumThreadManager() {
		this.ibatis = IbatisHelper.getSqlMapInstance();
	}
//------------------------------------manage Forum---------------------------------------------------------------------------	
	public List selectAll() {
		List result = null;
		try {
			result = ibatis.queryForList("forum.getThread","");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public ForumThreadModel selectUpdate(String id) {
		ForumThreadModel bean = new ForumThreadModel();
		try {
			bean = (ForumThreadModel) ibatis.queryForObject("forum.getThreadUpdate",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bean;
	}
	
	public List selectBatch() {
		List result = null;
		try {
			result = ibatis.queryForList("forum.getListBatch","");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List selectClass() {
		List result = null;
		try {
			result = ibatis.queryForList("forum.getListClass","");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean addForum(ForumThreadModel bean) {
		boolean result = true;   //Rere 17 Des 2013
		
		try {
			ibatis.startTransaction();
			this.ibatis.insert("forum.addForum", bean);
			this.ibatis.insert("forum.addDefaultComment", bean);
			ibatis.commitTransaction();
		} catch (SQLException sqe) {
			// TODO: handle exception
			result = false;  //Rere 17 Des 2013
			sqe.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return result;   //Rere 17 Des 2013
	}
	
	public boolean updateForum(ForumThreadModel bean) {
		boolean result = true;  //Rere 17 Des 2013
		
		try {
			ibatis.startTransaction();
			this.ibatis.update("forum.updateForum", bean);
			ibatis.commitTransaction();
		} catch (SQLException sqe) {
			// TODO: handle exception
			result = false;  //Rere 17 Des 2013
			sqe.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return result;  //Rere 17 Des 2013
	}

//--------------------------------------------------Thread---------------------------------------------------------------

	public List selectThread(String userid, String role) {
		List result = null;
		java.util.Map map = new HashMap();
		if(role.equalsIgnoreCase("trainee")){
			map.put("userid", userid);
		}
		try {
			result = ibatis.queryForList("forum.getThreadList",map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List selectComment(String id) {
		List result = null;
		try {
			System.out.println("id: " + id);
			result = ibatis.queryForList("forum.getCommentList",id);
			ForumCommentModel fcm = new ForumCommentModel();
			fcm = (ForumCommentModel) result.get(0);
			System.out.println("dtmpost aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: " + fcm.getDtmPost().toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public List selectSearch(String userid, String role, String keyword) {
		List result = null;
		java.util.Map map = new HashMap();
		map.put("keyword", keyword);
		if(role.equalsIgnoreCase("trainee")){
			map.put("userid", userid);
		}
		try {
			result = ibatis.queryForList("forum.getThreadList",map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("000000000000000000000000000000000000000000000000: " + result.size());
		return result;
	}
//--------------------------------------------------Comment---------------------------------------------------------------
	public boolean addComment(ForumCommentModel bean) {
		boolean result = true;   //Rere 17 Des 2013
		
		try {
			ibatis.startTransaction();
			this.ibatis.insert("forum.addComment", bean);
			ibatis.commitTransaction();
		} catch (SQLException sqe) {
			// TODO: handle exception
			result = false;   //Rere 17 Des 2013
			sqe.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return result;   //Rere 17 Des 2013
	}

	public boolean updateComment(ForumCommentModel bean) {
		boolean result = true;   //Rere 17 Des 2013
		
		try {
			ibatis.startTransaction();
			this.ibatis.update("forum.updateComment", bean);
			ibatis.commitTransaction();
		} catch (SQLException sqe) {
			// TODO: handle exception
			result = false;   //Rere 17 Des 2013
			sqe.printStackTrace();
		} finally {
			try {
				ibatis.endTransaction();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return result;   //Rere 17 Des 2013
	}
	
	public ForumCommentModel selectUpdateComment(String id) {
		ForumCommentModel bean = new ForumCommentModel();
		try {
			bean = (ForumCommentModel) ibatis.queryForObject("forum.getCommentUpdate",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bean;
	}
}
